import json
from .getPublicData import *


def getPieBrandData():
    cars = list(getAllCars())
    carsVolume = {}
    for car in cars:
        if carsVolume.get(car.brand,-1) == -1:
            # 量转 换成销量 数字类型
            carsVolume[str(car.brand)] = int(car.saleVolume)
        else:
            carsVolume[str(car.brand)] += int(car.saleVolume)
    print(carsVolume)
    carsVolume = sorted(zip(carsVolume.values(),carsVolume.keys()),reverse=True)
    sortDict = {car[1]: car[0] for car in carsVolume}
    lastPieList = []
    for k, v in sortDict.items():
        lastPieList.append({
            'name': k,
            'value': v
        })
    return lastPieList[:10]
